/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import models.Note;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Achie
 */
public class NoteDao {

    private final Session session;

    public NoteDao(Session session) {
        this.session = session;
    }

    public List<Note> findAll() {
        Transaction tx = session.beginTransaction();
        List<Note> list = session.createQuery("from Note").list();
        tx.commit();
        return list;
    }

    //get note objects by user id and lesson id
    public List<Note> findAllByUserAndLesson(long userid, long lessonid) {
        Transaction tx = session.beginTransaction();
        String sql = "SELECT id FROM note where user_id= :userid AND lesson_lesson_id= :lessonid";
        Query query = session.createSQLQuery(sql).setParameter("userid", userid).setParameter("lessonid", lessonid);
        List idlist = query.list();
        ArrayList<Note> list = new ArrayList<Note>();
        for (int i = 0; i < idlist.size(); i++) {
            query = session.createQuery("from Note where id= :id").setParameter("id", Long.parseLong(idlist.get(i).toString()));
            Note note = get(Long.parseLong(idlist.get(i).toString()));
            list.add(note);
        }
        tx.commit();
        return list;
    }
    //get note objects by user id (find all notes a user made)

    public List<Note> findNoteByUser(long userid) {
        Transaction tx = session.beginTransaction();
        String sql = "SELECT id FROM note where user_id= :userid";
        Query query = session.createSQLQuery(sql).setParameter("userid", userid);
        List idlist = query.list();
        ArrayList<Note> list = new ArrayList<Note>();
        for (int i = 0; i < idlist.size(); i++) {
            query = session.createQuery("from Note where id= :id").setParameter("id", Long.parseLong(idlist.get(i).toString()));
            Note note = get(Long.parseLong(idlist.get(i).toString()));
            list.add(note);
        }
        tx.commit();
        return list;
    }

    public boolean save(Note note) {
        try {
            Transaction tx = session.beginTransaction();
            session.save(note);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean update(Note note) {
        try {
            Transaction tx = session.beginTransaction();
            session.update(note);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public Note get(long id) {
        return (Note) session.get(Note.class, id);
    }

    public void delete(Note note) {
        Transaction tx = session.beginTransaction();
        session.delete(note);
        tx.commit();
    }
}
